package com.example.diskfilesystem.file;

import java.util.ArrayList;

public class openfile {
    private ArrayList<OFTLE> files;
    private int listLength;

    public openfile() {
        this.files = new ArrayList<>();
        this.listLength = 5;
    }

    //查找文件是否已打开，返回索引
    public int findOpenFile(String name) {
        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).getName().equals(name)) {
                return i;
            }
        }
        return -1;
    }

    //添加已打开文件
    public void addOpenFile(OFTLE oftle) {
        if (listLength == files.size()) {
            System.out.println("已打开文件数达到上限！");
            return;
        }
        files.add(oftle);
    }

    //关闭已打开文件
    public void closeOpenFile(int index) {
        if (index < 0 || index >= files.size()) {
            System.out.println("无效的文件索引！");
            return;
        }
        files.remove(index);
    }

    public int getListLength() {
        return listLength;
    }

    public void setListLength(int listLength) {
        this.listLength = listLength;
    }

    public ArrayList<OFTLE> getFiles() {
        return files;
    }

    public void setFiles(ArrayList<OFTLE> files) {
        this.files = files;
    }


}
